<template>
    <div class="post-lists">
        <el-card class="!border-none" shadow="never">
            <el-form
                ref="formRef"
                class="mb-[-16px]"
                @submit.prevent
                :model="queryParams"
                :inline="true"
            >
                <el-form-item label="名称">
                    <el-input
                        class="w-[280px]"
                        v-model="queryParams.name"
                        clearable
                        @keyup.enter="resetPage"
                    />
                </el-form-item>
                <el-form-item label="开户行">
                    <el-input
                        class="w-[280px]"
                        v-model="queryParams.bank"
                        clearable
                        @keyup.enter="resetPage"
                    />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                    <export
                        :params="queryParams"
                        v-perms="[`config.account/export`]"
                        name="导出"
                        api="finance.account/export"
                    />
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <div class="font-medium flex items-center">
                总金额：
                <div class="text-[#DC9B03] text-[30px] font-medium ff-BebasNeue mr-[20px]">
                    {{ getCurrency((pager.extend as any)?.sum || 0) }}
                </div>
            </div>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <common-table :columns="columns" :pager="pager">
                <template #btns>
                    <div class="">
                        <el-button
                            v-perms="['config.account/add']"
                            type="primary"
                            @click="handleAdd()"
                        >
                            <template #icon>
                                <icon name="el-icon-Plus" />
                            </template>
                            新增
                        </el-button>
                    </div>
                </template>
                <template #actions="{ row }">
                    <el-button
                        v-perms="['config.account/edit']"
                        type="primary"
                        @click="handleEdit(row)"
                    >
                        编辑
                    </el-button>
                    <el-button
                        v-perms="['config.account/delete']"
                        type="danger"
                        @click="handleDelete(row.id)"
                    >
                        删除
                    </el-button>
                </template>
            </common-table>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" @success="getLists" @close="showEdit = false" />
    </div>
</template>
<script lang="ts" setup name="OperationConfigClassifyLists">
import type { ColumnsType } from '@/components/common-table/types'
import feedback from '@/utils/feedback'

import EditPopup from './edit.vue'
import { apiConfigAccountDelete, apiConfigAccountLists } from '@/api/config.ts'
import { usePaging } from '@/hooks/usePaging.ts'
import { getCurrency } from '@/utils/util.ts'

const columns = reactive<ColumnsType[]>([
    {
        label: '开户名',
        prop: 'name',
        minWidth: '250',
        fixed: 'left'
    },
    {
        label: '账号',
        prop: 'account',
        minWidth: '220'
    },
    {
        label: '开户行',
        prop: 'bank',
        minWidth: '250'
    },
    {
        label: '公司',
        prop: 'company_name',
        minWidth: '250'
    },
    {
        label: '余额',
        prop: 'money',
        minWidth: '100',
        type: 'currency',
        renderCurrency(row, column) {
            return +row.money + +row.start_money
        },
    },
    {
        label: '余额更新时间',
        prop: 'last_time',
        type: 'date',
        fmt: 'yyyy-mm-dd hh:MM:ss',
        minWidth: '200'
    },
    {
        label: '添加时间',
        prop: 'ctime',
        type: 'date',
        fmt: 'yyyy-mm-dd hh:MM:ss',
        minWidth: '200'
    }
])

const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const showEdit = ref(false)
const queryParams = reactive({
    name: '',
    bank:''
})

const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: apiConfigAccountLists,
    params: queryParams
})

const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.getDetail(data)
}

const handleDelete = async (id: number) => {
    await feedback.confirm('确定要删除？')
    await apiConfigAccountDelete({ id })
    getLists()
}

onActivated(() => {
    getLists()
})
</script>
